﻿using System.Collections.Generic;
using System.Linq.Expressions;
using gbL.NetBoard.Examples.DoubleMVC;
using gbL.NetBoard.Examples.DoubleMVC.Controller;
using gbL.NetBoard.Examples.DoubleMVC.Controller.Utils;
using gbL.NetBoard.Examples.DoubleMVC.View.UI.Requests;
using NUnit.Framework;
using System;

namespace gbL.NetBoard.Examples.DoubleMVC.Tests
{
    [TestFixture]
    public class BrowseControllerTests
    {
        private class InputSource : IInputSource
        {
            public InputSource(string keys)
            {
                Keys = new Stack<char>(keys.ToCharArray());
            }

            public Stack<char> Keys { get; set; }

            public char GetKey()
            {
                return Keys.Pop();
            }

            public string GetLine()
            {
                throw new NotImplementedException();
            }
        }

        [Test]
        public void Default_should_request_browse_menu()
        {
            // A
            Blackboard domain = new Blackboard();
            BrowseController controller = new BrowseController(domain, new InputSource("X"));
            // A
            controller.Default();
            // A
            IList<IBlackboardEntry> messages = domain.Take(e => e.MessageType == typeof(MenuViewRequest));
            Assert.That(messages, Has.Count.EqualTo(1));
        }

        [Test]
        public void Default_should_request_products_action_on_P()
        {
            // A
            Blackboard domain = new Blackboard();
            BrowseController controller = new BrowseController(domain, new InputSource("P"));
            // A
            ControllerDispatch dispatch = controller.Default();
            // A
            MethodCallExpression methodExpr = (MethodCallExpression)dispatch.Target.Body;
            NewExpression newExpr = (NewExpression)methodExpr.Object;
            Assert.That(newExpr.Type, Is.EqualTo(typeof(BrowseController)));
            Assert.That(methodExpr.Method.Name, Is.EqualTo("Products"));
        }
    }
}
